package com.bjpowernode.web.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bjpowernode.api.service.UserService;
import com.bjpowernode.web.config.RealNameConfig;
import io.lettuce.core.output.BooleanOutput;
import javafx.scene.paint.PhongMaterial;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: panDeng_Zhu
 * @time: 2022/11/13 19:26
 */
@Service
public class RealNameService {

    @Resource
    private RealNameConfig realNameConfig;

    @DubboReference(interfaceClass = UserService.class, version = "1.0")
    private UserService userService;

    public boolean handleRealName(Integer userId, String name, String idCard) {
        //1.调用华为的接口
        boolean isOk = invokeRealName(name, idCard);
        //2.更新数据
        if (isOk) {
            userService.modifyRealName(userId, name, idCard);
        }
        return isOk;
    }

    public boolean invokeRealName(String name, String idCard) {
        boolean isOK = false;
        //使用HttpClient
        Map<String, Object> params = new HashMap<>();
        params.put("id_number", idCard);
        params.put("name", name);
        params.put("appKey", realNameConfig.getAppKey());

        try {
            String responseStr ="{\"code\":0," +
                    "\"message\":\"校验完成\"," +
                    "\"data\":" +
                    "   {\"remark\":\"一致\"," +
                    "   \"result\":1}" +
                    "   }";
            if (StringUtils.isNotBlank(responseStr)) {
                JSONObject json = JSONObject.parseObject(responseStr);
                if ("0".equals(json.getString("code"))) {
                    String detail = json.getJSONObject("data").getString("remark");
                    if ("一致".equals(detail)){
                        isOK=true;
                    }
                }
            }
        } catch (Exception e) {
            isOK = false;
            e.printStackTrace();
        }
        return isOK;
    }
}
